package com.example.demo.conf;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * gome.com.cn
 * Copyright (c) 2015-2025 All Rights Reserved.
 *
 * @author chaizhilei
 * @Description ES配置
 * @date 2020/9/17 20:35
 */
@Configuration
public class ESConfig {


    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient restHighLevelClient = null;
//        List<String> list = Arrays.asList("10.115.90.120:9201", "10.115.90.120:9202", "10.115.90.120:9203");
        List<String> list = Collections.singletonList("localhost:9200");
        HttpHost[] hosts = null;
            hosts = new HttpHost[list.size()];
            for (int i = 0; i < list.size(); i++) {
                String node = list.get(i);
                String[] split = node.split(":");
                hosts[i] = new HttpHost(split[0], Integer.valueOf(split[1]), "http");
            }
            RestClientBuilder builder = RestClient.builder(hosts);
            restHighLevelClient = new RestHighLevelClient(builder);

        return restHighLevelClient;
    }

}
